# 财务
# 添加应收款单16
# 合法添加收款时间（选择已收款）
from selenium import webdriver # 调用webdriver
from selenium.webdriver.common.by import By # 引入by模块
from time import sleep # 调用sleep
driver = webdriver.Chrome() #打开浏览器
try:
    driver.maximize_window()  # 最大化浏览器
    driver.get("http://192.168.1.5/crm") # 访问crm后台
    #定位器
    #登录
    name = "admin"
    password = "123456"
    name_locator = (By.NAME, 'name')  # 用户名
    password_locator = (By.NAME, 'password')  # 密码
    login_locator = (By.NAME, 'submit')  # 登录按钮

    #添加应收款单
    finance_locator = (By.LINK_TEXT, "财务")  # 定位财务
    add_ys_locator=(By.XPATH,"/html/body/div[5]/div[2]/div[1]/div[3]/a[2]")#添加应收单
    owner_name_locator=(By.ID,"owner_name")#负责人
    owner_radio_locator=(By.CSS_SELECTOR,"#d_content > tr:nth-child(2) > td:nth-child(1) > input[type=radio]")#选择负责人
    fzr_locator=(By.XPATH,"/html/body/div[8]/div[3]/div/button[1]") #ok按钮
    receivables_locator=(By.CSS_SELECTOR,"#receivables")#应收款
    field_ys_locator=(By.XPATH,"/html/body/div[7]/div[2]/table/tbody[2]/tr[7]/td[1]/input")#选择应收款徐大大
    ui_button_locator=(By.XPATH,"/html/body/div[7]/div[3]/div/button[1]")#点击ok
    money_locator=(By.ID,"money") #收款金额
    pay_time_locator=(By.ID,"pay_time")#收款时间
    status_locator=(By.NAME,"status")#选择未结账
    yi_status_locator=(By.XPATH,"/html/body/div[5]/div[2]/div/form/table/tbody/tr[7]/td[2]/input[2]")#选择已结账
    description_locator=(By.XPATH,"/html/body/div[5]/div[2]/div/form/table/tbody/tr[8]/td[2]/textarea")#描述
    submit_locator=(By.NAME,"submit")#保存

    #查找应收款单元素
    #登录
    driver.find_element(*name_locator).send_keys(name)  # 输入用户名
    sleep(1)
    driver.find_element(*password_locator).send_keys(password)  # 输入密码
    sleep(2)
    driver.find_element(*login_locator).click()  # 点击按钮
    #应收
    driver.find_element(*finance_locator).click()  # 点击财务
    driver.find_element(*add_ys_locator) .click() # 添加应收单
    driver.find_element(*owner_name_locator).click()#查找负责人元素，并点击
    sleep(2)
    driver.find_element(*owner_radio_locator).click()#查找按钮
    sleep(2)
    driver.find_element(*fzr_locator).click()#查找OK按钮元素，并点击
    sleep(2)
    # 查找元素
    driver.find_element(*receivables_locator).click()
    sleep(2)
    driver.find_element(*field_ys_locator).click() #查找应收款单元素
    sleep(2)
    driver.find_element(*ui_button_locator) .click()#查找ok元素，并点击
    sleep(2)
    driver.find_element(*money_locator).send_keys(5000)#查找收款金额元素，并输入内容
    pay_time_element=driver.find_element(*pay_time_locator)
    pay_time_element.clear()
    pay_time_element.send_keys("2022-09-30")
    # driver.find_element(*status_locator).click()#选择未结账元素，并点击
    driver.find_element(*yi_status_locator).click()  # 选择已结账元素，并点击
    driver.find_element(*description_locator).send_keys("下周回款") #查找描述,并输入内容
    sleep(2)
    driver.find_element(*submit_locator).click() #查找保存元素，并点击按钮
except:
    raise
finally:
    sleep(5) # 暂停五秒，并关闭浏览器
    driver.quit()